from dotenv import load_dotenv
import os

from langchain_community.graphs import Neo4jGraph

# Warning control
import warnings
warnings.filterwarnings("ignore")
load_dotenv('.env', override=True)
NEO4J_URI = os.getenv('NEO4J_URI')
NEO4J_USERNAME = os.getenv('NEO4J_USERNAME')
NEO4J_PASSWORD = os.getenv('NEO4J_PASSWORD')
NEO4J_DATABASE = os.getenv('NEO4J_DATABASE')

graph = Neo4jGraph(
    url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD, database=NEO4J_DATABASE
)

# # 查询所有节点
# cypher1 = """
#   MATCH (n)
#   RETURN count(n) as NumberOfNodes
#   """
# result1 = graph.query(cypher1)
# print(result1)
#
# # 引用方式示例
# cypher = """
#   MATCH (n)
#   RETURN count(n) AS numberOfNodes
#   """
# result = graph.query(cypher)
# result
# print(f"There are {result[0]['numberOfNodes']} nodes in this graph.")

# # 查询电影结点数量
# cypher2 = """
#   MATCH (m:Movie)
#   RETURN count(m) as NumberOfMovies
#   """
# result2 = graph.query(cypher2)
# print(result2)
#
# # 查询人物结点数量
# cypher3 = """
#   MATCH (people:Person)
#   RETURN count(people) as NumberOfPeople
#   """
# result3 = graph.query(cypher3)
# print(result3)
#
# # 查找人物Tom Hanks
# cypher4 = """
#   MATCH (tom:Person {name:"Tom Hanks"})
#   RETURN tom
#   """
# result4 = graph.query(cypher4)
# print(result4)
#
# # 查找电影Cloud Atlas
# cypher5 = """
#   MATCH (cloudAtlas:Movie{title:"Cloud Atlas"})
#   RETURN cloudAtlas
#   """
# result5 = graph.query(cypher5)
# print(result5)
#
# # 查找电影Cloud Atlas 但只返回其released属性
# cypher6 = """
#   MATCH (cloudAtlas:Movie{title:"Cloud Atlas"})
#   RETURN cloudAtlas.released
#   """
# result6 = graph.query(cypher6)
# print(result6)
#
# # 查找电影Cloud Atlas 返回其released属性和tagline属性
# cypher7 = """
#   MATCH (cloudAtlas:Movie{title:"Cloud Atlas"})
#   RETURN cloudAtlas.released,cloudAtlas.tagline
#   """
# result7 = graph.query(cypher7)
# print(result7)
#
# # 查询所有90年代的电影的标题(只返回了3条数据)
# cypher8 = """
#   MATCH (nineties:Movie)
#   WHERE nineties.released >= 1990 AND nineties.released < 2000
#   RETURN nineties.title
#   """
# result8 = graph.query(cypher8)
# print(result8)
#
# # 查询所有90年代的电影的标题(正确返回40条数据)
# cypher9 = """
#   MATCH (nineties:Movie)
#   WHERE nineties.released >= 1990 AND nineties.released < 2000
#   RETURN nineties.title as title
#   """
# # 执行查询
# result9 = graph.query(cypher9)
# # 打印所有结果
# movies = list(result9)  # 将结果转换为列表
# print(f"Found {len(movies)} movies from the 1990s:")
# for movie in movies:
#     print(movie["title"])

# # 查询演员出演的电影
# cypher10 = """
#   MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)
#   RETURN actor.name, movie.title LIMIT 10
#   """
# result10 = graph.run(cypher10)
# # # 打印所有结果
# kvs = list(result10)  # 将结果转换为列表
# for kv in kvs:
#     print(kv)
#
# # 查询TomHanks出演的电影
# cypher11 = """
#   MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies:Movie)
#   RETURN tom.name,tomHanksMovies.title
#   """
# result11 = graph.query(cypher11)
# kvs = list(result11)
# for kv in kvs:
#     print(kv)
#
# # 查询与TomHanks一起在各种电影中出演的人
# cypher12 = """
#   MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors)
#   RETURN coActors.name, m.title
#   """
# result12 = graph.run(cypher12)
# kvs = list(result12)
# for kv in kvs:
#     print(kv)
#
# # 查询到Emil Eifrem出演过的电影
# cypher13 = """
# MATCH (emil:Person {name:"Emil Eifrem"})-[actedIn:ACTED_IN]->(movie:Movie)
# RETURN emil.name, movie.title
# """
# result13 = graph.run(cypher13)
# print(result13)
#
# # 上一步查询到了Emil Eifrem只出演了电影The Matrix
# # 现在删除他的出演关系 查询后出现（No data）
# cypher14 = """
# MATCH (emil:Person {name:"Emil Eifrem"})-[actedIn:ACTED_IN]->(movie:Movie)
# DELETE actedIn
# """
# result14 = graph.run(cypher14)
# print(result14)
#
# # 创建新结点 这是一个人物结点 并且有name属性Andreas
# cypher15 = """
# CREATE (andreas:Person {name:"Andreas"})
# RETURN andreas
# """
# result15 = graph.run(cypher15)
# print(result15)
#
# # 为新结点Andreas和Emil Eifrem添加新关系
# cypher16 = """
# MATCH (andreas:Person {name:"Andreas"}), (emil:Person {name:"Emil Eifrem"})
# MERGE (andreas)-[hasRelationship:WORKS_WITH]->(emil)
# RETURN andreas, hasRelationship, emil
# """
# result16 = graph.run(cypher16)
# print(result16)
#